<?php

require_once 'db.php';
require_once 'works.php';

class pagination extends db {

    public function select($PER, $MAX) { ?>

        <?php $works = new works(); ?>
        <?php $page = $works->count() / PER_PAGE; ?>
        <?php for ($i = 1; $i <= $MAX; $i++): ?>
            <?php if (($_GET["page"] == 1) || ($_GET["page"] == 2) || ($_GET["page"] == 3) || ($_GET["page"] == 4)): ?>            
                <?php for ($i = 1; $i <= $MAX; $i++): ?>
                    <a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?>&nbsp;</a>
                    <?php if ($_GET["page"] == $i): ?>
                                               
                    <?php endif; ?>
                <?php endfor; ?>
                   
            <?php elseif (($_GET["page"] == $page) || ($_GET["page"] == $page - 1) || ($_GET["page"] == $page - 2) || ($_GET["page"] == $page - 3) || ($_GET["page"] == $page - 4)): ?>        
                <?php for ($i = $page - $MAX; $i <= $page; $i++): ?>
                    <a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?>&nbsp;</a>
                    <?php if ($_GET["page"] == $i): ?>
                        <?php $works->start = (($i - 1) * $works->page); ?>
                        <?php $works->page = $PER; ?>
                    <?php endif; ?>
                <?php endfor; ?>
                    
            <?php else : ?>
                <?php for ($i = $_GET["page"] - 4; $i <= $_GET["page"] + 5; $i++): ?>
                    <a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?>&nbsp;</a>
                    <?php if ($_GET["page"] == $i): ?>
                        <?php $works->start = (($i - 1) * $works->page); ?>
                        <?php $works->page = $PER; ?>
                    <?php endif; ?>
                <?php endfor; ?>
            <?php endif; ?> 
        <?php endfor; ?>
        <?php return $works->page ?>
        <?php return $works->start ?>            
        <?php return $_GET['page']; ?>           
<?php  }

}

?>
